package com.filldream.fastboot.web.controller.pc;

import com.filldream.fastboot.base.entity.SysAppResourcesHis;
import com.filldream.fastboot.base.request.IdSTO;
import com.filldream.fastboot.base.request.ModifyStatusSTO;
import com.filldream.fastboot.base.request.pc.serverInfo.*;
import com.filldream.fastboot.base.response.pc.appResource.AppResourcePageDTO;
import com.filldream.fastboot.common.annotation.Log;
import com.filldream.fastboot.common.annotation.RolePermissions;
import com.filldream.fastboot.common.entity.B;
import com.filldream.fastboot.common.entity.P;
import com.filldream.fastboot.common.entity.R;
import com.filldream.fastboot.core.service.SysAppResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@Api(tags = "PC-应用管理")
@RequestMapping("/pc/appResource")
public class PcSysAppResourceController {

    private final SysAppResourceService appResourceService;

    @Log(value = "新增应用")
    @ApiOperation("新增应用")
    @PostMapping("/add")
    @RolePermissions("appResource:add")
    public B add(@RequestBody @Validated AddAppResourceSTO addSTO){
        return B.checkSuccess(appResourceService.add(addSTO));
    }

    @Log(value = "获取应用")
    @ApiOperation("获取应用")
    @GetMapping("/get")
    @RolePermissions("appResource:get")
    public R get(IdSTO idSTO){
        return R.successd(appResourceService.getById(idSTO.getId()));
    }

    @Log(value = "修改应用")
    @ApiOperation("修改应用")
    @PostMapping("/modify")
    @RolePermissions("appResource:modify")
    public R modify(@RequestBody @Validated ModifyAppResourceSTO modifyAppResourceSTO){
        return R.successd(appResourceService.modify(modifyAppResourceSTO));
    }

    @ApiOperation("应用列表")
    @GetMapping("/page")
    @RolePermissions("appResource:page")
    public P<AppResourcePageDTO> page(AppResourcePageSTO appResourcePageSTO){
        return P.autoSuccess(appResourceService.queryPage(appResourcePageSTO));
    }

    @Log(value = "删除应用")
    @ApiOperation("删除应用")
    @PostMapping("/del")
    @RolePermissions("appResource:del")
    public R<Boolean> del(@RequestBody IdSTO idSTO){
        return R.successd(appResourceService.del(idSTO.getId()));
    }

    @Log(value = "启用|禁用应用")
    @ApiOperation("启用|禁用应用")
    @PostMapping("/modifyStatus")
    @RolePermissions("appResource:modifyStatus")
    public R<Boolean> modifyStatus(@RequestBody ModifyStatusSTO modifyStatusSTO){
        return R.successd(appResourceService.modifyStatus(modifyStatusSTO));
    }


    //--------------部署相关--------------

    @Log(value = "部署文件")
    @RolePermissions(value = "appResource:ftp")
    @ApiOperation("部署文件")
    @PostMapping("/ftp")
    public R<List<String>> ftp(@RequestBody FtpAppResourceSTO sto ){
        return R.successd(appResourceService.ftp(sto.getFileName(),sto.getAppId()));
    }

    @Log(value = "部署回调")
    @RolePermissions(value = "appResource:ftpCallBack")
    @ApiOperation("部署回调")
    @PostMapping("/ftpCallBack")
    public R<Boolean> ftpCallBack(@RequestBody FtpCallBackAppResourceSTO ftpCallBackAppResourceSTO ){
        return R.successd(appResourceService.ftpCallBack(ftpCallBackAppResourceSTO));
    }

    @Log(value = "停止服务")
    @RolePermissions(value = "appResource:startOrStop")
    @ApiOperation("停止服务")
    @PostMapping("/startOrStop")
    public R<Boolean> startOrStop(@RequestBody StartOrStopAppSTO startOrStopAppSTO ){
        return R.successd(appResourceService.startOrStop(startOrStopAppSTO));
    }

    @Log(value = "部署历史")
    @RolePermissions(value = "appResource:ftpHis")
    @ApiOperation("部署历史")
    @GetMapping("/ftpHis")
    public R<List<SysAppResourcesHis>> ftpHis(AppHisPageSTO pageSTO){
        return R.autoSuccess(appResourceService.ftpHis(pageSTO));
    }

    @Log(value = "回退版本")
    @RolePermissions(value = "appResource:ftpBack")
    @ApiOperation("回退版本")
    @PostMapping("/ftpBack")
    public R<List<String>> ftpBack(@RequestBody  IdSTO idSTO){
        return R.successd(appResourceService.ftpBack(idSTO.getId()));
    }


}